<template>
    <div class="app-container">
        <el-row>
            <el-col :span="24" class="card-box">
                <el-card>

                    <el-button type="warning" plain icon="Download" @click="handleUpdateOrderStatus"
                        v-hasPermi="['medicine:order:edit']">重新提交审核</el-button>
                    <el-button type="warning" plain icon="Download" @click="handleBack">
                        返回</el-button>
                    <div class="el-table el-table--enable-row-hover el-table--medium"></div>
                </el-card>
            </el-col>
        </el-row>
    </div>

    <el-col :span="24" class="card-box">
        <el-card>
            <template #header>
                <Monitor style="width: 1em; height: 1em; vertical-align: middle" />
                <span style="vertical-align: middle">采购单信息</span>
            </template>
            <el-form :model="orderForm" ref="queryRef" v-show="showSearch" label-width="68px">
                <el-form-item label="订单ID" prop="orderId">
                    <el-input disabled v-model="orderForm.orderId" placeholder="请输入订单ID" clearable />
                </el-form-item>
                <el-form-item label="订单编号" prop="orderNum">
                    <el-input disabled v-model="orderForm.orderNum" placeholder="请输入订单编号" clearable />
                </el-form-item>
                <el-form-item label="供应商" prop="orderSupplier" style="width: 200px">
                    <el-select v-model="orderForm.supplierId" placeholder="请选择供应商">
                        <el-option v-for="dict in supplier_info" :key="dict" :label="dict.supplierName"
                            :value="dict.supplierId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="orderForm.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleUpdateOrder">确认更新</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </el-col>

    <el-col :span="24" class="card-box">
        <el-card>
            <template #header>
                <Monitor style="width: 1em; height: 1em; vertical-align: middle" />
                <span style="vertical-align: middle">订单详情</span>
            </template>
            <div class="el-table el-table--enable-row-hover el-table--medium">
                <div class="el-table el-table--enable-row-hover el-table--medium">
                    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                        <el-form-item label="批次号" prop="detailBatch">
                            <el-input v-model="queryParams.detailBatch" placeholder="请输入批次号" clearable />
                        </el-form-item>
                        <el-form-item label="药品名称" prop="drugName">
                            <el-input v-model="queryParams.drugName" placeholder="请输入药品名称" clearable />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>

                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button type="warning" plain icon="Download" @click="handleExport"
                                v-hasPermi="['medicine:detail:export']">导出</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                                v-hasPermi="['medicine:order:remove']">删除</el-button>
                        </el-col>
                        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                    </el-row>

                    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="详情ID" align="center" prop="detailId" />
                        <el-table-column label="药品名称" align="center" prop="hisMedicineDrug.drugName" />
                        <el-table-column label="数量" align="center" prop="detailNum">
                            <template #default="scope">
                                <el-input-number v-model="scope.row.detailNum" min="1" size="small"
                                    @change="updateNum(scope.row)" />
                            </template>
                        </el-table-column>
                        <el-table-column label="订单ID" align="center" prop="orderId" />
                        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                            <template #default="scope">
                                <span>{{
                                    parseTime(scope.row.createTime, "{y}-{m}-{d}")
                                    }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注" align="center" prop="remark" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button link type="primary" icon="Edit" @click="handleDelete(scope.row)"
                                    v-hasPermi="['medicine:orderDetail:edit']">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize" @pagination="getList" />
                </div>
            </div>
        </el-card>
    </el-col>
</template>

<script setup name="Detail">
import { listSupplierAll } from "@/api/his/supplier";
import { getOrder, updateOrder } from "@/api/his/order";

import {
    listDetail,
    getDetail,
    delDetail,
    addDetail,
    updateDetail,
} from "@/api/his/detail";
import { get } from "@vueuse/core";

const { proxy } = getCurrentInstance();

const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const route = useRoute();
const router = useRouter();

const orderId = route.params.orderId;

const data = reactive({
    orderForm: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId,
        detailBatch: null,
    },
    rules: {
        detailNum: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        detailMoney: [
            { required: true, message: "药品单价不能为空", trigger: "blur" },
        ],
    },
});

const { queryParams, orderForm, rules } = toRefs(data);

const supplier_info = ref([]);
//返回
function handleBack() {
    router.push("/medicine/order");
}
//更新订单的采购状态
function handleUpdateOrderStatus() {
    const data = {
        orderId: orderForm.value.orderId,
        orderStatus: 0,
    };
    proxy.$modal.confirm('是否重新审核，编号为"' + orderForm.value.orderNum + '"的采购单？').then(function(){
        return updateOrder(data);
    }).then(() => {
        proxy.$modal.msgSuccess("提交审核成功");
        getOrderInfo();
    });
    
}
//异步加载供应商信息
listSupplierAll().then((res) => {
    supplier_info.value = res.rows;
});
//渲染数据
function getOrderInfo() {
    getOrder(orderId).then((response) => {
        orderForm.value = response.data;
    });
}
// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.detailId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
getOrderInfo();
/** 查询订单详情信息列表 */
function getList() {
    loading.value = true;
    // queryParams.value.orderId = orderId;
    listDetail(queryParams.value).then((response) => {
        detailList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

/** 修改采购信息 */
function handleUpdateOrder() {
    updateOrder(orderForm.value).then((res) => {
        proxy.$modal.msgSuccess("修改成功");
        getOrderInfo();
    });
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        "medicine/detail/export",
        {
            ...queryParams.value,
        },
        `detail_${new Date().getTime()}.xlsx`
    );
}
//更新采购项的数量
function updateNum(row) {
    const data = {
        detailId: row.detailId,
        detailNum: row.detailNum,
    };
    updateDetail(data).then((res) => {
        if (res.code == 200) {
            proxy.$modal.msgSuccess("数量修改成功");
        }
    });
}
/** 删除按钮操作 */
function handleDelete(row) {
    const _detailIds = row.detailId || ids.value;
    proxy.$modal
        .confirm('是否确认删除采购项编号为"' + _detailIds + '"的数据项？')
        .then(function () {
            return delDetail(_detailIds);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}

getList();
</script>
